﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_TrainingCoursesDtls : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        InitialData_BLL iniData = new InitialData_BLL();
        IJobSiteBLL jBLL = new JobSiteBLL();
        JobSiteInfo info = new JobSiteInfo();
        ConvertNullable cn = new ConvertNullable();
        TrainingCoursesInfo courseInfo = new TrainingCoursesInfo();
        TrainingCoursesDAL courseDAL = new TrainingCoursesDAL();
        ITrainingCoursesQuotaBLL qBLL = new TrainingCoursesQuotaBLL();
        ITrainingCoursesTrainerBLL tBLL = new TrainingCoursesTrainerBLL();
        ITrainingLecturerSkillsBLL lSkillBLL = new TrainingLecturerSkillsBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {               
                LoadDataGrid3();
                LoadDataGrid1();
                LoadDataGrid2();
                LoadDetail();
                LoadJobSite();
                LoadCBTrainer();
            }
        }

    //================================== ข้อมูลหลัก ======================================
        protected void LoadDetail()
        {
            int train_seq = Convert.ToInt32(Convert.ToInt32(Request.QueryString["Sequence"].ToString()));
            TrainingCoursesInfo info = courseDAL.GetData(train_seq);
            lbTrainingCourseDescr.Text = info.training_course_descr;
            //lbTotalScore.Text = info.total_score.ToString();
            lbTotalPrice.Text = info.total_price.ToString();
            //lbDateSignStard.Text = info.date_sign_start.ToString(); ต้องไม่เป็นค่าว่าง
            if (info.date_sign_start.ToString().Length > 0)
            { lbDateSignStard.Text = info.date_sign_start.Value.ToShortDateString(); }
            //if (info.time_sign_end.ToString().Length > 0)
            if (info.time_sign_end != null && info.time_sign_end != "")             //if (info.time_sign_end != null)
            { lbTimeSignEnd.Text = info.time_sign_end.Substring(0, 5); }
            if (info.date_sign_end.ToString().Length > 0)
            { lbDateSignEnd.Text = info.date_sign_end.Value.ToShortDateString(); }
            if (info.date_training_start.ToString().Length > 0)
            { lbDateTrainStard.Text = info.date_training_start.Value.ToShortDateString(); }
            if (info.date_training_end.ToString().Length > 0)
            { lbDateTrainEnd.Text = info.date_training_end.Value.ToShortDateString(); }          
            lbTotalPrice.Text = info.total_price.ToString();
            //lbTotalScore.Text = info.total_score.ToString();
            lbMaxTrainee.Text = info.max_trainee_count.ToString();
            //lbSchedule.Text = info.schedule_text.ToString();
            if (info.schedule_text != null)
            { lbSchedule.Text = info.schedule_text; }
        }

    //============================================= โควต้าหน่วยงาน =======================================================    
        private void LoadDataGrid2()
        {
            string CourseSeq = Request.QueryString["Sequence"].ToString();            
            //string CourseSeq = "3";
            RadGrid2.DataSource = qBLL.GetFull(CourseSeq);
            RadGrid2.DataBind();
            SumQuota();
        }
        private void SumQuota()
        {
            int QuotaCount = 0;
            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                GridDataItem item = RadGrid2.Items[i];
                int qItem = Convert.ToInt32(item["quota"].Text);
                QuotaCount = QuotaCount + qItem;
            }
            lbQuota.Text = QuotaCount.ToString();
        }
        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                UpdateUI2("AddNew");
            }
            else if (e.CommandName == "DataEdit")
            {
                txtQuotaSeq.Text = RadGrid2.Items[e.Item.ItemIndex]["course_quota_seq"].Text;
                cbJobSite.SelectedValue = RadGrid2.Items[e.Item.ItemIndex]["job_site_seq"].Text;
                txtquota.Text = RadGrid2.Items[e.Item.ItemIndex]["quota"].Text;
                UpdateUI2("Update");
            }
            else if (e.CommandName == "Delete")
            {

                int Seq = Convert.ToInt32(RadGrid2.Items[e.Item.ItemIndex]["course_quota_seq"].Text);
                //ssMng.EmpSeqI
                qBLL.Delete(Seq, ssMng.EmpSeqI);
            }
            LoadDataGrid2();
        }
        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
           
        }
        protected void LoadJobSite()
        {
            IList<JobSiteInfo> infoList = jBLL.ListJobSiteByTypes(SystemInfo._JobsiteInternal, "3");
            cbJobSite.DataSource = infoList;
            cbJobSite.DataBind();
        }   
        protected void btnSave2_Click(object sender, EventArgs e)
        {
            bool sta = courseDAL.chkDupTrainingQuta(Request.QueryString["Sequence"].ToString(), cbJobSite.SelectedValue);
            if (sta != true)
            {
                //Request.QueryString["Sequence"].ToString();
                //ssMng.EmpSeqI
                qBLL.Insert(txtquota.Text, Convert.ToInt32(Request.QueryString["Sequence"].ToString())
                            , cbJobSite.SelectedValue, ssMng.EmpSeqI, DateTime.Today);
                LoadDataGrid2();
                UpdateUI2("Save");
            }
            else
            { lblError2.Text = "หน่วยงานที่เลือกมีในระบบแล้ว กรุณาเลือกหน่วยงานใหม่"; }
            lblError2.Text = "";
        }
        protected void btnUpdate2_Click(object sender, EventArgs e)
        {
            //bool sta = courseDAL.chkDupTrainingQuta(Request.QueryString["Sequence"].ToString(), cbJobSite.SelectedValue);
            //if (sta != true)
            //{
                //Request.QueryString["Sequence"].ToString();
                //ssMng.EmpSeqI
                int QuotaSeq = Convert.ToInt32(txtQuotaSeq.Text);
                qBLL.Update(QuotaSeq, txtquota.Text, Convert.ToInt32(Request.QueryString["Sequence"].ToString()), cbJobSite.SelectedValue
                            , ssMng.EmpSeqI, DateTime.Today);
                LoadDataGrid2();
                UpdateUI2("Save");
            //}
            //else
            //{ lblError2.Text = "หน่วยงานที่เลือกมีในระบบแล้ว กรุณาเลือกหน่วยงานใหม่"; }
            //lblError2.Text = "";
        }
        protected void btnCancel2_Click(object sender, EventArgs e)
        {
            UpdateUI2("Cancel");
            ClearControl();
        }
        //======================== User Contorl2  =====================
        protected void ClearControl()
        {
            lblError2.Text = " ";
        }
        
        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid2.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid2.Visible = false;
                    PanelAddData2.Visible = true;
                    btnUpdate2.Visible = false;
                    btnSave2.Visible = true;
                    txtquota.Text = string.Empty;

                    break;

                case "Cancel":
                    PanelGrid2.Visible = true;
                    PanelAddData2.Visible = false;
                    //lblError2.Text = "";

                    break;

                case "Update":
                    PanelGrid2.Visible = false;
                    PanelAddData2.Visible = true;
                    btnSave2.Visible = false;
                    btnUpdate2.Visible = true;

                    break;
            }
        }
    //========================================== รายชื่อวิทยากร  =============================================  
      
        private void LoadDataGrid1()
        {
            //Request.QueryString["Sequence"].ToString();
            RadGrid1.DataSource = tBLL.GetFull(Request.QueryString["Sequence"].ToString());
            RadGrid1.DataBind();
        }

        private void LoadCBTrainer()
        {
            //Request.QueryString["Sequence"].ToString();
            cbTrainer.DataSource = lSkillBLL.GetBySubject(Request.QueryString["Sequence"].ToString());
            cbTrainer.DataBind();
        }
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                UpdateUI1("AddNew");
            }
            else if (e.CommandName == "DataEdit")
            {
                txtQuotaSeq.Text = RadGrid2.Items[e.Item.ItemIndex]["course_quota_seq"].Text;
                cbJobSite.SelectedValue = RadGrid2.Items[e.Item.ItemIndex]["job_site_seq"].Text;
                txtquota.Text = RadGrid2.Items[e.Item.ItemIndex]["quota"].Text;
                UpdateUI2("Update");
            }
            else if (e.CommandName == "Delete")
            {
                int Seq = Convert.ToInt32(RadGrid1.Items[e.Item.ItemIndex]["trainer_seq"].Text);
                //ssMng.EmpSeqI
                tBLL.Delete(Seq, ssMng.EmpSeqI);
            }
            LoadDataGrid1();
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }

        protected void rblAddPreson_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }
        protected void btnSave1_Click(object sender, EventArgs e)
        {
            if (checkValidat1() == true)
            {
                //Request.QueryString["Sequence"].ToString();
                //ssMng.EmpSeqI
                tBLL.Insert(cn.ConvertNullInt(cbTrainer.SelectedValue), Convert.ToInt32(Request.QueryString["Sequence"].ToString()), txtRemark.Text, cn.ConvertNullInt((ssMng.EmpSeq)));
                LoadDataGrid1();
                UpdateUI1("Save");
            }
        }
        protected void btnUpdate1_Click(object sender, EventArgs e)
        {
            //Request.QueryString["Sequence"].ToString();
            //ssMng.EmpSeqI
            //int QuotaSeq = Convert.ToInt32(txtQuotaSeq.Text);
            //qBLL.Update(QuotaSeq, txtquota.Text, 3, cbJobSite.SelectedValue, 70, DateTime.Today);
            //LoadDataGrid1();
            //UpdateUI2("Save");
        }
        protected void btnCancel1_Click(object sender, EventArgs e)
        {
            UpdateUI1("Cancel");
            ClearControl();
        }
        //======================== User Contorl1  =====================
        protected void ClearControl1()
        {
        }
        protected bool checkValidat1()
        {
            bool bOK = true;
            //string strJobSite = cbJobSite.SelectedValue;
            //string strQuota = txtquota.Text;

            //if (strJobSite.Length == 0)
            //{
            //    bOK = false;
            //    lblError1.Text = "* กรุณาเลือกหน่วยงาน";
            //}
            return bOK;
        }
        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid1.Visible = true;
                    PanelAddData1.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid1.Visible = false;
                    PanelAddData1.Visible = true;
                    btnUpdate1.Visible = false;
                    btnSave1.Visible = true;

                    break;

                case "Cancel":
                    PanelGrid1.Visible = true;
                    PanelAddData1.Visible = false;

                    break;

                case "Update":
                    PanelGrid1.Visible = false;
                    PanelAddData1.Visible = true;
                    btnSave1.Visible = false;
                    btnUpdate1.Visible = true;

                    break;
            }
        }
    //================================== แจ้งเวียน  ======================================        
       
        private void LoadDataGrid3()
        {
            string CourseSeq = Request.QueryString["Sequence"].ToString();
            RadGrid3.DataSource = qBLL.GetFull(CourseSeq);
            RadGrid3.DataBind();
        }
        protected void RadGrid3_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
        
        }
        protected void ddlEmployeeType_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }
        
        protected void cb1_Checked(object sender, EventArgs e)
        { 
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
           
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //if (checkValidate() == true)
            //{
               
            //}

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI2("Cancel");
            ClearControl();
        }
    

       
    }

}
